package ae.pcfc.cldos.online.web.controller.client.company.general;

import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import ae.pcfc.cldos.online.common.delegate.CompanyInformationDelegate;
import ae.pcfc.cldos.online.common.exception.GSAppException;
import ae.pcfc.cldos.online.common.exception.NoSuchCompanyException;
import ae.pcfc.cldos.online.common.vo.CompanyInformationVO;
import ae.pcfc.cldos.online.common.vo.selectors.SystemValuesVO;
import ae.pcfc.cldos.online.web.Constants;
import ae.pcfc.cldos.online.web.ViewPath;
import ae.pcfc.cldos.online.web.controller.base.BasePageController;
import ae.pcfc.cldos.online.web.model.CompanyInformationModel;

/**
 * Controller that handles all {@link Company}-related requests, i.e.: all
 * requests for /company/register/nonclient/
 * 
 * @author Abdullah
 */
@Controller
@RequestMapping(value = ViewPath.__COMPANY_GENERAL_INFO)
public class CompanyGeneralController extends BasePageController {
	@Autowired
	@Qualifier("companyInformationDelegate")
	private CompanyInformationDelegate companyInformationDelegate;
	
	@ModelAttribute
	public void populateBreadCrumbs(ModelMap model) {
		breadCrumbsFactoryService.populateGeneralData(model);
	}
	
	@ModelAttribute(Constants.__MEDICAL_TYPE_MAP_MODEL_KEY)
	public Map<String, String> populateMedicalTypeList(Locale locale){
		
		Map<String, String> result = new HashMap<String, String>();
		
		List<SystemValuesVO> medicalTypes = systemValuesDelegate.getMedicalType();
		
		for(SystemValuesVO medicalType :medicalTypes ){
			if(locale.getLanguage().equals("ar")){
				result.put(medicalType.getSvCode(), medicalType.getSvDescArb());
			}else {
				result.put(medicalType.getSvCode(), medicalType.getSvDesc());
			}
		}
		
		return result;
	}

	@ModelAttribute("CompanyInformationInfo")
	public CompanyInformationModel populateCompanyInformationInfoModel(HttpServletRequest request) {
		CompanyInformationModel companyInformationModel = new CompanyInformationModel();
		CompanyInformationVO companyInformationVO=null;
		try{
			companyInformationVO = companyInformationDelegate.retriveGSInformation(getUserVO());
			mapper.map(companyInformationVO, companyInformationModel);
		}catch(NoSuchCompanyException e){
			companyInformationModel.setCompanyError(true);
		}catch(GSAppException e){
			companyInformationModel.setGsError(true);
		}
		return companyInformationModel;
	}
	
	@RequestMapping(value = "/show.htm", method = RequestMethod.GET)
	public void show(){
		
	}



}
